﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using Utilities;

namespace MaHoaUngDung
{
    static class Program
    {
        public static User CurrentUser;
        public static UserList ListUser;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            CurrentUser = null;

            //load database
            XmlSerializer serializer = new XmlSerializer(typeof(UserList));
            TextReader reader = null;
            ListUser = new UserList();
            try
            {
                reader = new StreamReader(Utility.XmlUserFilePath);
                ListUser = (UserList)serializer.Deserialize(reader);
            }
            catch (Exception)
            {
                MessageBox.Show("Cannot load user database.", "database not found",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            Application.Run(new DangNhapFrm());

            //save database
            string start_up = Directory.GetCurrentDirectory() + "\\users.xml";
            TextWriter writer = new StreamWriter(start_up, false);
            //TextWriter writer = new StreamWriter(Utility.XmlUserFilePath, false);
            try
            {
                serializer.Serialize(writer, ListUser);
            }
            catch (Exception)
            {
                MessageBox.Show("Error occurs when saving database.", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            finally
            {
                writer.Close();
            }
        }
    }
}
